package acmain	;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;
import javax.swing.JFrame;
import javax.swing.Timer;

import model.GameModel;
import engine.GameEngine;
import gui.GUIComponent;

public class Game extends JFrame implements ComponentListener {

	private static final long serialVersionUID = -6915920684052555961L;
	
	/* basic variables */
	final private Boolean fullscreenOnStart = false;
	public Boolean fullscreen; //altering this wont change behaviour
	
	/* get and set "screen size" */
	Toolkit currentToolkit = Toolkit.getDefaultToolkit();
	Dimension screenSize = currentToolkit.getScreenSize();
	public int resolutionWidth = screenSize.width;
	public int resolutionHeight = screenSize.height;
	
	/* basic declarations */
	private GUIComponent gui;
	private GameModel model;
	private GameEngine engine;
	
	/* clock */
	private Timer timer;

	
	public Game() {
		
		/* initialization */
		this.setTitle("APOC");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setSize(new Dimension(resolutionWidth, resolutionHeight));
		this.setResizable(false);
		this.setLayout(new BorderLayout());
		this.addKeyListener(new KeyHandler());
		this.addMouseListener(new MouseHandler());
		this.addMouseMotionListener(new MouseMotionHandler());
	
		/* objects */
		model = new GameModel();
		engine = new GameEngine(model);
		gui = new GUIComponent(model);
		
		/* functions */
		this.add(gui, BorderLayout.CENTER);
		if(fullscreenOnStart) {
			this.makeFullscreen();
		}else
		{
			this.removeFullscreen();
		}
		this.setVisible(true);
	}

	/* fullscreen handling functions */
	public void makeFullscreen() {
		this.dispose();
		this.setUndecorated(true);
		this.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
		this.pack();
		this.setBounds(0, 0, resolutionWidth, resolutionHeight); 
		this.setVisible(true);
		fullscreen = true;
	}
	
	public void removeFullscreen() {
		this.dispose();
		this.setUndecorated(false);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.pack();
		this.setBounds(0, 0, resolutionWidth, resolutionHeight);
		this.setVisible(true);
		fullscreen = false;
	}

	public void switchFullscreen() {
		System.out.println("fullscreen:" + fullscreen);
		if(fullscreen){
			removeFullscreen();
		} 
		else {
			makeFullscreen();
		}
	}
	
	public static void main(String[] args) {
		Game test = new Game();
		System.out.println(test.getContentPane().getWidth());
		System.out.println(test.getContentPane().getHeight());
	}

	/**
	 * A listener that handles key events
	 */
	class KeyHandler extends KeyAdapter {
		public void keyPressed(KeyEvent event) {
			if(event.getKeyCode() == 112){
				switchFullscreen();
				
				
			}
			engine.handleKeyPressed(event);
		}

		public void keyReleased(KeyEvent event) {
			engine.handleKeyPressed(event);
		}
		
		
	}

	/**
	 * A listener that handles mousebuttons events
	 */
class MouseHandler extends MouseAdapter {
		public void mousePressed(MouseEvent event) {
			engine.handleMousePressed(event);
		}

		public void mouseReleased(MouseEvent event) {
			engine.handleMouseReleased(event);
		}
	}
	
	/**
	 * A listener that handles mouse-movements
	 */
	class MouseMotionHandler extends MouseMotionAdapter {
		public void mouseMoved(MouseEvent event) {
			engine.handleMouseMoved(event);
		}

		public void mouseDragged(MouseEvent event) {
			engine.handleMouseDragged(event);
		}
	}
	
	
	/**
	 * A listener that handles timer events
	 */
	class TimerHandler implements ActionListener {
		public void actionPerformed(ActionEvent event) {
			//engine.clocktick();
			//notifiera gui att rita om
		}
	}
	
	public void startClock(int milliseconds) {
		if (timer != null && timer.isRunning()) {
			timer.stop();
		}
		timer = new Timer(milliseconds, new TimerHandler());
		timer.start();
	}

	public void stopClock() {
		this.timer.stop();
	}
	
	public boolean hasRunningClock() {
		return timer.isRunning();
	}
	
	
	

	public void componentHidden(ComponentEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void componentMoved(ComponentEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void componentResized(ComponentEvent arg0) {
		// TODO Auto-generated method stub
	}

	public void componentShown(ComponentEvent arg0) {
		// TODO Auto-generated method stub
	}

}
